import random

from ttzScrapyProject.constants import UrlConstant
from ttzScrapyProject.dao.BaseDao import MysqlDao
from ttzScrapyProject.utils import HttpUtils
import time

from ttzScrapyProject.utils.WeChatUtils import send_message

"""
 换卡
"""
class ExchangeDao(MysqlDao):
    def __init__(self):
        super().__init__()
        self.cursor = self.getCursor()
        self.db = self.getDB()
        self.tableName = 'exchange'


    def getExchangeNum(self, userToken):
        # 获取今日换卡次数-火蚁卡数量
        exchangeNumUrl = f"{UrlConstant.EXCHANGE_CARD_NUM_URL}{userToken}&client=app&Type=2&Number=1&ExchangeType=1"
        exchangeNumInfo = HttpUtils.get("获取换卡次数", exchangeNumUrl)
        if len(exchangeNumInfo) ==0:
            return 0
        exchangeNumInfoData = exchangeNumInfo['Data']
        print(f"今日换卡次数为{exchangeNumInfoData['Number']}")
        num = int(exchangeNumInfoData['Number'])
        if num == 0:
            return 0
        else:
            return num

    """
        滚动条流程
        
    """
    def compose(self, userToken,loginUserVo):
        # 中奖,and信息获取
        composeUrl = f"{UrlConstant.EXCHANGE_CARD_COMPOSE_URL}{userToken}&client=app&Type=1&Num=1"
        composeData = HttpUtils.get("抽取动作", composeUrl)
        number = composeData['Data']['Number']
        if number == '0':
            print("抽取失败")
            return
        # 获取抽取信息
        time.sleep(random.randint(1, 3))
        infoUrl = f"{UrlConstant.EXCHANGE_CARD_COMPOSE_INFO_URL}{userToken}&client=app&Type=1&Num=1"
        infoData = HttpUtils.get("获取中奖信息", infoUrl)

        # if infoData['Data']['FreecaCanCnt'] !='0':
        #     print("获取中奖信息失败")
        #     return
        # 合成卡片
        time.sleep(random.randint(1, 3))
        self.composeGoods(userToken,loginUserVo)
        # http://webapicname.coo456.com/apiNewLottery/lottery?token=18f240213f74ded319be83ffef76131f&client=app&Type=1&Num=1
        # http://webapicname.coo456.com/apiNewLottery/info?token=18f240213f74ded319be83ffef76131f&client=app

    """
        获取所有可合成商品列表
    """
    def getComposeList(self, userToken):
        # http://webapicname.coo456.com/apiNewLottery/composeList?token=f667e96528012cf7987a1eae0a075e57&client=app
        composeListUrl = f"{UrlConstant.EXCHANGE_CARD_COMPOSE_LIST}{userToken}&client=app"
        composeListInfo = HttpUtils.get("获取可合成商品列表", composeListUrl)
        composeListInfoLists = composeListInfo['Data']['List']
        result = [compose for compose in composeListInfoLists if compose['Name'] == '火蚁卡']

        return result[0]['Id']

    def composeGoods(self, userToken,loginUserVo):
        # http://webapicname.coo456.com/apiNewLottery/compose?token=f667e96528012cf7987a1eae0a075e57&client=app&GoodsId=444&Number=1
        GoodsId = self.getComposeList(userToken)
        time.sleep(random.randint(1, 3))
        composeGoodsUrl = f"{UrlConstant.EXCHANGE_CARD_COMPOSE_GOODS}{userToken}&client=app&GoodsId={GoodsId}&Number=1"
        composeGoodsInfo = HttpUtils.get("合成火蚁卡", composeGoodsUrl)
        composeGoodsInfoData = composeGoodsInfo['Data']
        time.sleep(random.randint(1, 3))
        print(f"合成成功，获得{composeGoodsInfoData['Number']}张火蚁卡")
        send_message(loginUserVo.userNoticeList, f"用户{loginUserVo.phone}-合成成功，获得{composeGoodsInfoData['Number']}张火蚁卡")
